import tkinter as tk
from tkinter import messagebox

class LoginDialog:
    def __init__(self, parent, on_success):
        self.parent = parent
        self.on_success = on_success
        self.create_login_dialog()

    def create_login_dialog(self):
        self.dialog = tk.Toplevel(self.parent)
        self.dialog.title("登录")
        self.dialog.geometry("300x150")
        self.dialog.resizable(False, False)
        self.dialog.transient(self.parent)  # Make the login dialog modal

        tk.Label(self.dialog, text="用户名:").pack(pady=5)
        self.username_entry = tk.Entry(self.dialog)
        self.username_entry.pack(pady=5)

        tk.Label(self.dialog, text="密码:").pack(pady=5)
        self.password_entry = tk.Entry(self.dialog, show="*")
        self.password_entry.pack(pady=5)

        tk.Button(self.dialog, text="登录", command=self.validate_login).pack(pady=10)

    def validate_login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()

        if username == "root" and password == "root":
            self.on_success()
        else:
            messagebox.showerror("错误", "用户名或密码错误")

if __name__ == "__main__":
    root = tk.Tk()
    root.withdraw()  # Hide the root window

    def on_login_success():
        messagebox.showinfo("成功", "登录成功")
        root.destroy()

    LoginDialog(root, on_login_success)
    root.mainloop()
